package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.WorkDTO;
import com.fuli.app.dto.query.WorkQueryDTO;
import com.fuli.app.service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(tags ="作业")
@RestController
@RequestMapping("/work")
public class WorkController {
    @Autowired
    private WorkService workService;

    public static final long MAX_SIZE = 1024 * 512;
    public static final Set<String> IMAGE_SUFFIX_SET = new HashSet<>(Arrays.asList(
            "jpg", "JPG", "jpge", "JPGE", "png", "PNG", "gif", "GIF"));


    @ApiOperation(
            value = "新增作业分类",
            notes = "<b>必传属性：</b><br/>" +
                    "1.image 作业 拍照图片形式上传<br/>" +
                    "2.curriculumId 课程id<br/>" +
                    "3.content 对作业图片的额外描述<br/>" +
                    "4.exerciseId 习题id<br/>" +
                    "5.userId 用户id<br/>")
    @PostMapping(value = "/add")
    public ActionResult<WorkDTO> add(@ApiParam("作业图片") @RequestParam("image") MultipartFile file,
                                     @ApiParam("课程id") @RequestParam String curriculumId,
                                     @ApiParam("对作业图片的额外描述（选填）") @RequestParam String content,
                                     @ApiParam("习题id") @RequestParam String exerciseId,
                                     @ApiParam("用户id") @RequestParam String userId) {
        if (file.getSize() > MAX_SIZE)
            return new ActionResult<>(0, "图片大小超过限制");
        if (file.isEmpty())
            return new ActionResult<>(0, "文件不存在");
        int lastIndex = file.getOriginalFilename().lastIndexOf('.');
        if (lastIndex == -1)
            return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
        String suffix = file.getOriginalFilename().substring(lastIndex + 1);
        if (StringUtils.isEmpty(suffix))
            return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
        if (!IMAGE_SUFFIX_SET.contains(suffix))
            return new ActionResult<>(0, "文件格式不支持，仅支持jpg,jpge,png,gif格式");
        if (StringUtils.isEmpty(curriculumId))
            return new ActionResult<>(0, "课程id为空");
        if (StringUtils.isEmpty(exerciseId))
            return new ActionResult<>(0, "习题id为空");
        if (StringUtils.isEmpty(userId))
            return new ActionResult<>(0, "用户id为空");

        return workService.add(file, curriculumId, content, exerciseId, userId);
    }

    @ApiOperation(value = "删除作业分类")
    @DeleteMapping(value = "/delete/{id}")
    public ActionResult<String> delete(@ApiParam("作业分类id") @PathVariable String id) {
        return workService.delete(id);
    }

    @ApiOperation(value = "批量删除作业")
    @DeleteMapping(value = "/batchDelete")
    public ActionResult<List<String>> batchDelete(@ApiParam("作业id数组") @RequestParam List<String> ids) {
        if (CollectionUtils.isEmpty(ids))
            return new ActionResult<>(0, "参数不能为空");
        return workService.batchDelete(ids);
    }

    @ApiOperation(value = "获取作业")
    @GetMapping(value = "/get/{id}")
    public ActionResult<WorkDTO> get(@ApiParam("作业id") @PathVariable String id) {
        return workService.get(id);
    }

    @ApiOperation(value = "通过课程id获取作业")
    @GetMapping(value = "/getByCurriculumId/{curriculumId}")
    public ActionResult<List<WorkDTO>> getByCurriculumId(@ApiParam("课程id") @PathVariable String curriculumId) {
        return workService.getByCurriculumId(curriculumId);
    }
    @ApiOperation(value = "通过习题id获取作业")

    @GetMapping(value = "/getByExerciseId/{exerciseId}")
    public ActionResult<List<WorkDTO>> getByExerciseId(@ApiParam("习题id") @PathVariable String exerciseId) {
        return workService.getByExerciseId(exerciseId);
    }

    @ApiOperation("查询指定条件的作业")
    @GetMapping("/pageQuery")
    public ActionResult<Pagination<WorkDTO>> pageQuery(@ApiParam("作业查询DTO") @ModelAttribute WorkQueryDTO queryDTO) {
        //验证页码
        if (queryDTO.getPageNo() == null || queryDTO.getPageNo() <= 0)
            queryDTO.setPageNo(1);
        if (queryDTO.getPageSize() == null || queryDTO.getPageSize() <= 0)
            queryDTO.setPageSize(10);
        return workService.listByQueryDTO(queryDTO);
    }

    @ApiOperation("标记处理")
    @PutMapping(value = "/deal{id}")
    public ActionResult<String> deal(@ApiParam("id") @PathVariable String id) {
        return workService.deal(id);
    }

    @ApiOperation("批量标记处理")
    @PutMapping(value = "/batchDeal")
    public ActionResult<List<String>> batchDeal(@ApiParam("作业id数组") @RequestParam List<String> ids) {
        return workService.batchDeal(ids);
    }

    @ApiOperation("作业添加成绩")
    @PutMapping(value = "/grade")
    public ActionResult<String> grade(@ApiParam("id") @RequestParam String id,
                                      @ApiParam("分数") @RequestParam String grade) {
        if (StringUtils.isEmpty(id))
            return new ActionResult<>(0, "id为空");
        if (StringUtils.isEmpty(grade))
            return new ActionResult<>(0, "成绩为空");
        return workService.grade(id, grade);
    }
}
